package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.DeleteRequest;
import com.yupi.springbootinit.mapper.QaInspectionInstrumentMapper;
import com.yupi.springbootinit.model.dto.qa.QaInspectionInstrumentAddRequest;
import com.yupi.springbootinit.model.dto.qa.QaInspectionInstrumentQueryRequest;
import com.yupi.springbootinit.model.dto.qa.QaInspectionInstrumentUpdateRequest;
import com.yupi.springbootinit.model.entity.QaInspectionInstrument;
import com.yupi.springbootinit.service.QaInspectionInstrumentService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【qaInspectionInstrument(本次检验使用的计量器具)】的数据库操作Service实现
* @createDate 2024-02-25 17:04:02
*/
@Service
public class QaInspectionInstrumentServiceImpl extends ServiceImpl<QaInspectionInstrumentMapper, QaInspectionInstrument>
        implements QaInspectionInstrumentService {

    @Override
    public long addQaInspectionInstrument(QaInspectionInstrumentAddRequest qaInspectionInstrumentAddRequest) {
        QaInspectionInstrument qaInspectionInstrument = new QaInspectionInstrument();
        BeanUtils.copyProperties(qaInspectionInstrumentAddRequest, qaInspectionInstrument);
        this.save(qaInspectionInstrument);
        return qaInspectionInstrument.getId();
    }

    @Override
    public boolean deleteQaInspectionInstrument(DeleteRequest deleteRequest) {
        return this.removeById(deleteRequest.getId());
    }

    @Override
    public boolean updateQaInspectionInstrument(QaInspectionInstrumentUpdateRequest qaInspectionInstrumentUpdateRequest) {
        QaInspectionInstrument qaInspectionInstrument = new QaInspectionInstrument();
        BeanUtils.copyProperties(qaInspectionInstrumentUpdateRequest, qaInspectionInstrument);
        return this.updateById(qaInspectionInstrument);
    }

    @Override
    public QaInspectionInstrument getQaInspectionInstrumentById(long id) {
        return this.getById(id);
    }

    @Override
    public List<QaInspectionInstrument> listQaInspectionInstrument(QaInspectionInstrumentQueryRequest qaInspectionInstrumentQueryRequest) {
        QueryWrapper<QaInspectionInstrument> queryWrapper = getQueryWrapper(qaInspectionInstrumentQueryRequest);
        return this.list(queryWrapper);
    }

    @Override
    public Page<QaInspectionInstrument> listQaInspectionInstrumentByPage(QaInspectionInstrumentQueryRequest qaInspectionInstrumentQueryRequest) {
        long current = qaInspectionInstrumentQueryRequest.getCurrent();
        long size = qaInspectionInstrumentQueryRequest.getPageSize();
        Page<QaInspectionInstrument> page = new Page<>(current, size);
        QueryWrapper<QaInspectionInstrument> queryWrapper = getQueryWrapper(qaInspectionInstrumentQueryRequest);
        return this.page(page, queryWrapper);
    }

    private QueryWrapper<QaInspectionInstrument> getQueryWrapper(QaInspectionInstrumentQueryRequest qaInspectionInstrumentQueryRequest) {
        QueryWrapper<QaInspectionInstrument> queryWrapper = new QueryWrapper<>();
        if (qaInspectionInstrumentQueryRequest == null) {
            return queryWrapper;
        }
        Long id = qaInspectionInstrumentQueryRequest.getId();
        if (id != null && id > 0) {
            queryWrapper.eq("id", id);
        }
        return queryWrapper;
    }
}